package com.xzh.homeliving.commodity.service.impl;

import com.xzh.homeliving.commodity.dao.SpuInfoDao;
import com.xzh.homeliving.commodity.entity.SpuInfoEntity;
import com.xzh.homeliving.commodity.vo.SearchResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzh.common.utils.PageUtils;
import com.xzh.common.utils.Query;

import com.xzh.homeliving.commodity.dao.SkuInfoDao;
import com.xzh.homeliving.commodity.entity.SkuInfoEntity;
import com.xzh.homeliving.commodity.service.SkuInfoService;

import javax.annotation.Resource;


/**
 * @author xzh
 */
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Resource
    private SpuInfoDao spuInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();
        // 获取检索关键字
        String key = (String) params.get("key");
        // 获取分类 id
        String catalogId = (String)params.get("catalogId");
        // 获取品牌 id
        String brandId = (String)params.get("brandId");
        // 获取价格最大值
        String min = (String) params.get("min");
        // 获取最小值
        String max = (String)params.get("max");
        if(StringUtils.isNotBlank(key)) {
            skuInfoEntityQueryWrapper.and((obj) -> obj.eq("sku_id", key).or().like("sku_name", key));
        }
        if(StringUtils.isNotBlank(catalogId) && !"0".equalsIgnoreCase(catalogId)) {
            skuInfoEntityQueryWrapper.eq("catalog_id", catalogId);
        }
        if(StringUtils.isNotBlank(brandId) && !"0".equalsIgnoreCase(brandId)) {
            skuInfoEntityQueryWrapper.eq("brand_id", brandId);
        }
        if(StringUtils.isNotBlank(min)) {
            skuInfoEntityQueryWrapper.ge("price", min);
        }
        if(StringUtils.isNotBlank(max)) {
            skuInfoEntityQueryWrapper.le("price", max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                skuInfoEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public SearchResultVo querySearchPageByCondition(Map<String, Object> params) {
        // 先查询已上架的spuInfoEntity
        List<SpuInfoEntity> publishedSpu= spuInfoDao.selectList(new QueryWrapper<SpuInfoEntity>().eq("publish_status", 1));
        List<Long> spuInfoIds = publishedSpu.stream().map(SpuInfoEntity::getId).collect(Collectors.toList());

        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();
        String keyword = (String)params.get("keyword");
        String catalogId = (String) params.get("catalog3Id");
        String brandId = (String)params.get("brandId");

        if(!spuInfoIds.isEmpty()) {
            skuInfoEntityQueryWrapper.in("spu_id", spuInfoIds);
        } else {
            SearchResultVo searchResultVo = new SearchResultVo();
            searchResultVo.setTotal(0L);
            searchResultVo.setPageNum(1);
            searchResultVo.setTotalPages(0);
            searchResultVo.setPageNavs(new ArrayList<>());
            searchResultVo.setCommodity(new ArrayList<>());
            searchResultVo.setKeyword(params.get("keyword") == null ? "" : params.get("keyword").toString());
            return searchResultVo;
        }

        if(StringUtils.isNotBlank(keyword)) {
            skuInfoEntityQueryWrapper.and(obj -> obj.eq("sku_id", keyword).or().like("sku_name", keyword));
        }
        if(StringUtils.isNotBlank(catalogId) && !"0".equalsIgnoreCase(catalogId)) {
            skuInfoEntityQueryWrapper.eq("catalog_id", catalogId);
        }
        if(StringUtils.isNotBlank(brandId)) {
            skuInfoEntityQueryWrapper.eq("brand_id", brandId);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                skuInfoEntityQueryWrapper
        );
        PageUtils pageUtils =  new PageUtils(page);
        SearchResultVo searchResultVo = new SearchResultVo();
        searchResultVo.setCommodity((List<SkuInfoEntity>)pageUtils.getList());
        searchResultVo.setPageNum(pageUtils.getCurrPage());
        searchResultVo.setTotal((long)pageUtils.getTotalCount());
        int totalPage = pageUtils.getTotalPage();
        searchResultVo.setTotalPages(totalPage);
        List<Integer> pageNavs = new ArrayList<>();
        // 遍历总的页数，得到 pageNavs
        for(int i = 1; i <= totalPage; i++) {
            pageNavs.add(i);
        }
        searchResultVo.setPageNavs(pageNavs);
        searchResultVo.setKeyword(params.get("keyword") == null ? "" : params.get("keyword").toString());
        return searchResultVo;
    }
}
